package dongshi.daddy.jh.changeGridName_kongjie;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ChangeGridByAddrMain {

    public static void main(String[] args) throws Exception {
//        generateShanghuSql();
        generateZhuSql();
    }

    private static void generateZhuSql() throws Exception {
        String fileName = "D:\\work\\每个月的工作\\2023\\孔姐修改网格名称\\根据地址修改网格\\住户信息.txt";
        List<String> addrList = new ArrayList<>();
        List<String> gridList = new ArrayList<>();
        boolean addrArea = true;
        boolean gridArea = false;
        try (Scanner sc = new Scanner(new FileReader(fileName))) {
            while (sc.hasNextLine()) {  //按行读取字符串
                String line = sc.nextLine().trim();
                if (line.contains("所属网格")) {
                    gridArea = !gridArea;
                    addrArea = !addrArea;
                }
                if (addrArea) addrList.add(line);
                if (gridArea) gridList.add(line);
            }
        }
        String updateSql = "update tbl_jh_form_business_db22_DebLc5Wm set `f436d8a558ec4b66bb5ff4d8db17a5bd`='newGridName' where `e6a79bd1ec3e4faa8365eeab05cec84d`='addrVal';";
        String dir = "D:\\work\\每个月的工作\\2023\\孔姐修改网格名称\\根据地址修改网格\\zhuhu_update_sql.txt";
        File file = new File(dir);
        //如果文件不存在，创建文件
        if (!file.exists())
            file.createNewFile();
        //创建FileWriter对象
        FileWriter writer = new FileWriter(file);
        for (int i = 1; i < addrList.size(); i++) {
            String finalSql = updateSql.replaceAll("newGridName", gridList.get(i));
            finalSql = finalSql.replaceAll("addrVal", addrList.get(i));
            System.out.println(finalSql);
            //向文件中写入内容
            writer.write(finalSql + "\n");

        }
        writer.flush();
        writer.close();

    }
    
    private static void generateShanghuSql() throws Exception {
        String fileName = "D:\\work\\每个月的工作\\2023\\孔姐修改网格名称\\根据地址修改网格\\商户信息.txt";
        List<String> addrList = new ArrayList<>();
        List<String> gridList = new ArrayList<>();
        boolean addrArea = true;
        boolean gridArea = false;
        try (Scanner sc = new Scanner(new FileReader(fileName))) {
            while (sc.hasNextLine()) {  //按行读取字符串
                String line = sc.nextLine().trim();
                if (line.contains("所属网格")) {
                    gridArea = !gridArea;
                    addrArea = !addrArea;
                }
                if (addrArea) addrList.add(line);
                if (gridArea) gridList.add(line);
            }
        }
        String updateSql = "update tbl_jh_form_business_db22_v6BBCHvv set `e8aaa52d635c42a9b2136a339097ecb9`='newGridName' where `e4bf615db8444c4e855a8e3417f5d41e`='addrVal';";
        String dir = "D:\\work\\每个月的工作\\2023\\孔姐修改网格名称\\根据地址修改网格\\shanghu_update_sql.txt";
        File file = new File(dir);
        //如果文件不存在，创建文件
        if (!file.exists())
            file.createNewFile();
        //创建FileWriter对象
        FileWriter writer = new FileWriter(file);
        for (int i = 1; i < addrList.size(); i++) {
            String finalSql = updateSql.replaceAll("newGridName", gridList.get(i));
            finalSql = finalSql.replaceAll("addrVal", addrList.get(i));
            System.out.println(finalSql);
            //向文件中写入内容
            writer.write(finalSql + "\n");

        }
        writer.flush();
        writer.close();

    }
}
